package templateDesigner

import org.grails.plugins.wsclient.service.WebService
import groovy.xml.MarkupBuilder

class TemplateService {
    
    static transactional = true
    WebService webService
    
    //--------------------------------------------------------------------------
    
    def getJerarquiaDetallesArtefacto() {
        
        def wsdlURL = 'http://localhost:8080/ArtefactRepository-OpenEHRGen/services/templates?wsdl'
        def proxy = webService.getClient(wsdlURL)

        def result = proxy.getJerarquiaDetallesArtefacto()       
        return result 
    }
        
    //--------------------------------------------------------------------------
    
    def getTemplate(String n, String v, String p) {
        
        def wsdlURL = 'http://localhost:8080/ArtefactRepository-OpenEHRGen/services/templates?wsdl'
        def proxy = webService.getClient(wsdlURL)

        def result = proxy.getTemplate(n,v,p)       
        return result 
    }
    
    //--------------------------------------------------------------------------
    
    def saveTemplate(Edicion edic) {
        
        def wsdlURL = 'http://localhost:8080/ArtefactRepository-OpenEHRGen/services/templates?wsdl'
        def proxy = webService.getClient(wsdlURL)

        def result = proxy.saveTemplate(edic.versionTemplate.template.path, edic.versionTemplate.template.nombre, edic.versionTemplate.template.identif, edic.versionTemplate.numero, edic.xmlTemplate.encodeAsBase64().toString())       
        return result 
    }
    
    //--------------------------------------------------------------------------
    
    // Crea el XML de un Template Vacio, con id, nombre y layout
    String crearXMLTemplateInicial(String idTemplate, String nombreTemplate, String idLayout){
        
        def writer = new StringWriter()
        def builder = new MarkupBuilder(writer)

        builder.template() {
            id(idTemplate)
            name(nombreTemplate)
            layout(idLayout)
            root_archetype("")
        }
        
        return writer.toString()
    }
    
    //--------------------------------------------------------------------------
    
    String crearXMLTemplate(def datosTemplate){
        
        def pathLayout = datosTemplate.pathLayout
        def nombreArchivoLayout = datosTemplate.nombreArchivoLayout
        def versionLayout = datosTemplate.versionLayout
        def nombreTemplate = datosTemplate.nombreTemplate
        def nombreArchivoTemplate = datosTemplate.nombreArchivoTemplate
        def versionTemplate = datosTemplate.versionTemplate
        
        String pageZoneArchetipeRoot =  getPageZone(datosTemplate)
        
        def writer = new StringWriter()
        def builder = new MarkupBuilder(writer)
        builder.template(){
            id(datosTemplate.nombreArchivoTemplate)
            layout(path: datosTemplate.pathLayout, nombreArchivo: datosTemplate.nombreArchivoLayout, version: datosTemplate.versionLayout)
            root_aechetype(type: getType(datosTemplate.archetypeRoot), id: datosTemplate.archetypeRoot + ".v" + datosTemplate.archetypeRootVersion, pageZone: pageZoneArchetipeRoot){
                if (pageZoneArchetipeRoot != "none"){
                    for (n in datosTemplate.zones[pageZoneArchetipeRoot].value.nodes){
                        campo(path: n)
                    }
                }
            }
            included_archetypes(){
                for (z in datosTemplate.zones){
                    archetype(type: getType(z.value.archetype), id: z.value.archetype + ".v" + z.value.archetypeVersion, pageZone: z.key, includeAll: "false"){
                        for (p in z.value.nodesArchetype){
                            campo(path: p)
                        }    
                    }
                }
            }
        }
        
        return writer.toString().replace("campo","field") // El replace es porque el builder no deje poner un tag con nombre field
        
    }
    
    //--------------------------------------------------------------------------
    
    //--------
    
    private String getType(String idArtefacto){       
        //openEHR-EHR-SECTION.prehospitalario
        
        def prefijosIdArtefacto = idArtefacto.split('\\.')[0].split('-');
        def tipo = prefijosIdArtefacto[prefijosIdArtefacto.length - 1]
        return tipo
        //return "AAA"
    }                
         
    //--------
           
    private String getPageZone(def datosTemplate){
        
        String pageZones = "none"
        
        datosTemplate.zones.each{zone ->
            if (zone.value.nodes != []){
                pageZones = zone.key
            }
        }

        return pageZones
    }            
    
    //--------------------------------------------------------------------------
}
